package cn.com.nes.site.controller.crm.opportunity;

import cn.com.nes.common.web.CommonResult;
import cn.com.nes.mybatis.agent.crm.entity.Opportunity;
import cn.com.nes.site.controller.crm.opportunity.vo.*;
import cn.com.nes.site.modules.opportuntiy.OpportunityBusiness;
import cn.com.nes.site.service.crm.opportunity.CrmOpportunityService;
import cn.com.nes.site.service.crm.opportunity.OpportunityAreaCodeService;
import cn.com.nes.site.service.crm.opportunity.RelationOpportunityService;
import cn.com.nes.site.service.crm.opportunity.bo.GetOpportunityDetailBO;
import cn.com.nes.site.service.crm.opportunity.bo.OpportunityBO;
import cn.com.nes.site.service.crm.opportunity.bo.RelationOpportunityBO;
import cn.com.nes.site.service.crm.opportunity.dto.OpportunityAndContractDTO;
import cn.com.nes.site.service.crm.opportunity.dto.OpportunityDetailDTO;
import cn.com.nes.site.service.crm.opportunity.dto.OpportunityInfoDto;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

/**
 * 商机接口
 */
@Slf4j
@Api(tags = {"销售管理-商机管理"})
@RestController
@RequestMapping("/v2/crm/opportunity")
public class OpportunityController {

    @Resource
    private CrmOpportunityService crmOpportunityService;

    @Resource
    private RelationOpportunityService relationOpportunityService;

    @Resource
    private OpportunityBusiness opportunityBusiness;

    @Resource
    private OpportunityAreaCodeService opportunityAreaCodeService;

    @ApiOperation(value = "查询商机详情")
    @RequestMapping(value = "getOpportunityDetail", method = RequestMethod.GET)
    public CommonResult<OpportunityDetailDTO> getOpportunityDetail(
            @Validated GetOpportunityDetailVO vo) throws Exception {
        return CommonResult.data(opportunityBusiness.details(vo.convertToBo(new GetOpportunityDetailBO())));
    }

    @ApiOperation("商机列表-分页")
    @RequestMapping(value = "/getOpportunityPage", method = RequestMethod.GET)
    public CommonResult<OpportunityInfoDto> getOpportunityPage(
            QueryOpportunityPageVo queryOpportunityPageVo) throws Exception {
        return CommonResult.page(opportunityBusiness.page(queryOpportunityPageVo.convertToBo()));
    }

    @ApiOperation(value = "保存商机")
    @RequestMapping(value = "saveOpportunity", method = RequestMethod.POST)
    public CommonResult<?> saveOpportunity(@RequestBody @Validated SaveOpportunityVO vo) throws Exception {
        OpportunityBO opportunityBO = vo.convertToOpportunityBO();
        return CommonResult.data(opportunityBusiness.save(opportunityBO).getUuid());
    }

    @ApiOperation(value = "获取客户的商机列表")
    @RequestMapping(value = "getOpportunities", method = RequestMethod.GET)
    public CommonResult<List<Opportunity>> getOpportunities(QueryOpportunityVO vo) throws Exception {
        return CommonResult.data(crmOpportunityService.getOpportunities(vo.convertToQueryOpportunityBo()));
    }

    @ApiOperation(value = "商机成交")
    @RequestMapping(value = "opportunitySuccess", method = RequestMethod.POST)
    public CommonResult<?> opportunitySuccess(
            @RequestBody @Validated OpprotuniySuccessVO opprotuniySuccessVO) throws Exception {
        crmOpportunityService.opportunitySuccess(opprotuniySuccessVO.convertToBo());
        return CommonResult.ok();
    }

    @ApiOperation(value = "查询商机列表", notes = "，包含合同信息")
    @RequestMapping(value = "getOpportunitiesContainContract", method = RequestMethod.GET)
    public CommonResult<?> getOpportunitiesContainContract(QueryOpportunityVO vo) throws Exception {
        return CommonResult.data(crmOpportunityService.getOpportunitiesContainContract(vo.convertToQueryOpportunityBo()));
    }

    @ApiOperation(value = "关联主从商机方法", notes = "商机管理->打开商机->商机列表从商机数据")
    @RequestMapping(value = "relationOpportunity", method = RequestMethod.POST)
    public CommonResult<?> relationOpportunity(@RequestBody @Validated RelationOpportunityVO vo) throws Exception {
        relationOpportunityService.relationOpportunity(vo.convertToBo(new RelationOpportunityBO()));
        return CommonResult.ok("操作成功");
    }

    @ApiOperation(value = "查询关联商机的列表", notes = "查询商机列表，根据主id查从商机，商机管理->打开商机->选择已关联商机的列表")
    @RequestMapping(value = "getRelationOpportunityPage", method = RequestMethod.GET)
    public CommonResult<OpportunityAndContractDTO> getRelationOpportunityPage(
            RelationOpportunityPageVO vo) throws Exception {
        return CommonResult.page(relationOpportunityService.getRelationOpportunity(vo));
    }

    @ApiOperation(value = "删除关联商机方法", notes = "商机管理->打开商机->删除商机关联方法")
    @RequestMapping(value = "delRelationOpportunity", method = RequestMethod.POST)
    public CommonResult<RelationOpportunityVO> delRelationOpportunity(
            @RequestBody @Validated DelRelationOpportunityVO vo) throws Exception {
        relationOpportunityService.delRelationOpportunity(vo.convertToBo(new RelationOpportunityBO()));
        return CommonResult.ok("删除成功");
    }

    @ApiOperation(value = "查询区划代码树", notes = "用于回显区划代码")
    @RequestMapping(value = "getRegionCodeTree", method = RequestMethod.GET)
    public CommonResult<?> getRegionCodeTree(QueryOpportunityVO vo) throws Exception {
        if (StrUtil.isEmpty(vo.getOpportunityId())) {
            return CommonResult.error("商务项目");
        }
        return CommonResult.data(opportunityAreaCodeService.getRegionCodeTreeForOpportunity(vo.getOpportunityId()));
    }

    @ApiOperation(value = "查询区划代码", notes = "用于回显区划代码")
    @RequestMapping(value = "getRegionCodesForOpportunity", method = RequestMethod.GET)
    public CommonResult<?> getRegionCodesForOpportunity(QueryOpportunityVO vo) throws Exception {
        if (StrUtil.isEmpty(vo.getOpportunityId())) {
            return CommonResult.error("商务项目");
        }
        if (StrUtil.isEmpty(vo.getAreaCode())) {
            return CommonResult.error("行政区划");
        }
        return CommonResult.data(opportunityAreaCodeService.getRegionCodesForOpportunity(vo.getOpportunityId(), vo.getAreaCode()));
    }

}
